home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / URLEncoder.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  126 lines

  1. /*
  2.  * @(#)URLEncoder.java    1.12 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.net;
  16.  
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.OutputStreamWriter;
  19. import java.io.IOException;
  20. import java.util.BitSet;
  21.  
  22. /**
  23.  * The class contains a utility method for converting a 
  24.  * <code>String</code> into a MIME format called 
  25.  * "<code>x-www-form-urlencoded</code>" format. 
  26.  * <p>
  27.  * To convert a <code>String</code>, each character is examined in turn:
  28.  * <ul>
  29.  * <li>The ASCII characters '<code>a</code>' through '<code>z</code>', 
  30.  *     '<code>A</code>' through '<code>Z</code>', and '<code>0</code>' 
  31.  *     through '<code>9</code>' remain the same. 
  32.  * <li>The space character '<code> </code>' is converted into a 
  33.  *     plus sign '<code>+</code>'. 
  34.  * <li>All other characters are converted into the 3-character string 
  35.  *     "<code>%<i>xy</i></code>", where <i>xy</i> is the two-digit
  36.  *     hexadecimal representation of the lower 8-bits of the character.
  37.  * </ul>
  38.  *
  39.  * @author  Herb Jellinek
  40.  * @version 1.12, 07/01/98
  41.  * @since   JDK1.0
  42.  */
  43. public class URLEncoder {
  44.     static BitSet dontNeedEncoding;
  45.     static final int caseDiff = ('a' - 'A');
  46.  
  47.     /* The list of characters that are not encoded have been determined by 
  48.        referencing O'Reilly's "HTML: The Definitive Guide" (page 164). */
  49.        
  50.     static {
  51.     dontNeedEncoding = new BitSet(256);
  52.     int i;
  53.     for (i = 'a'; i <= 'z'; i++) {
  54.         dontNeedEncoding.set(i);
  55.     }
  56.     for (i = 'A'; i <= 'Z'; i++) {
  57.         dontNeedEncoding.set(i);
  58.     }
  59.     for (i = '0'; i <= '9'; i++) {
  60.         dontNeedEncoding.set(i);
  61.     }
  62.     dontNeedEncoding.set(' '); /* encoding a space to a + is done in the encode() method */
  63.     dontNeedEncoding.set('-');
  64.     dontNeedEncoding.set('_');
  65.     dontNeedEncoding.set('.');
  66.     dontNeedEncoding.set('*');
  67.     }
  68.  
  69.     /**
  70.      * You can't call the constructor.
  71.      */
  72.     private URLEncoder() { }
  73.  
  74.     /**
  75.      * Translates a string into <code>x-www-form-urlencoded</code> format.
  76.      *
  77.      * @param   s   <code>String</code> to be translated.
  78.      * @return  the translated <code>String</code>.
  79.      * @since   JDK1.0
  80.      */
  81.     public static String encode(String s) {
  82.     int maxBytesPerChar = 10;
  83.     ByteArrayOutputStream out = new ByteArrayOutputStream(s.length());
  84.     ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
  85.     OutputStreamWriter writer = new OutputStreamWriter(buf);
  86.  
  87.     for (int i = 0; i < s.length(); i++) {
  88.         int c = (int)s.charAt(i);
  89.         if (dontNeedEncoding.get(c)) {
  90.         if (c == ' ') {
  91.             c = '+';
  92.         }
  93.         out.write(c);
  94.         } else {
  95.         // convert to external encoding before hex conversion
  96.         try {
  97.             writer.write(c);
  98.             writer.flush();
  99.         } catch(IOException e) {
  100.             buf.reset();
  101.             continue;
  102.         }
  103.         byte[] ba = buf.toByteArray();
  104.         for (int j = 0; j < ba.length; j++) {
  105.             out.write('%');
  106.             char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
  107.             // converting to use uppercase letter as part of
  108.             // the hex value if ch is a letter.
  109.             if (Character.isLetter(ch)) {
  110.             ch -= caseDiff;
  111.             }
  112.             out.write(ch);
  113.             ch = Character.forDigit(ba[j] & 0xF, 16);
  114.             if (Character.isLetter(ch)) {
  115.             ch -= caseDiff;
  116.             }
  117.             out.write(ch);
  118.         }
  119.         buf.reset();
  120.         }
  121.     }
  122.  
  123.     return out.toString();
  124.     }
  125. }
  126.